package com.exam.ch8;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;

public class TestColumnModel {
	
	public TestColumnModel(){
	
		JFrame f = new JFrame();
	 
	   	JTable t=new JTable(new MyTableModel());
	   
	   	JComboBox c = new JComboBox();
	   	c.addItem("Taipei");
	   	c.addItem("ChiaYi");
	   	c.addItem("HsinChu");
	   	t.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(c));
	   	t.setPreferredScrollableViewportSize(new Dimension(550, 30));
	        
	   	JScrollPane s = new JScrollPane(t);
	        
	    f.getContentPane().add(s, BorderLayout.CENTER);
		f.setTitle("ColumnModelTest");
	    f.pack();
	    f.setVisible(true);
	        
	    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	
	class MyTableModel extends AbstractTableModel{

        Object[][] p = {
            {"阿呆", "Taipei",new Integer(66), 
              new Integer(32), new Integer(98), new Boolean(false),new Boolean(false)},
            {"阿瓜", "ChiaYi",new Integer(85), 
              new Integer(69), new Integer(154), new Boolean(true),new Boolean(false)},          
        };

        String[] n = {"姓名", 
        	         "居住地",
                      "语文",
                      "数学",
                      "总分",
                      "及格",			         
                      "作弊"};
    
        public int getColumnCount() {
            return n.length;
        }

        public int getRowCount() {
            return p.length;
        }

        public String getColumnName(int col) {
            return n[col];
        }

        public Object getValueAt(int row, int col) {
            return p[row][col];
        }
	    public Class getColumnClass(int c) {
        	return getValueAt(0, c).getClass();
    	}
	    public boolean isCellEditable(int rowIndex, int columnIndex) {
		    return true;	
    	}
	    
	    public void setValueAt(Object value, int row, int col) {
        	p[row][col] = value;
		    fireTableCellUpdated(row, col);
    	}
	}
	
	public static void main(String[] args) {
		
		new TestColumnModel();
		
	}

}
